package com.hngates.project.controller;

import java.util.List;

import com.hngates.common.utils.ShiroUtils;
import com.hngates.project.domain.ConstructionContract;
import com.hngates.project.domain.IdName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hngates.common.annotation.Log;
import com.hngates.common.enums.BusinessType;
import com.hngates.project.domain.ConstructionOrganization;
import com.hngates.project.service.IConstructionOrganizationService;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.utils.poi.ExcelUtil;
import com.hngates.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 施工单位Controller
 * 
 * @author ruoyi
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/project/organization")
public class ConstructionOrganizationController extends BaseController
{
    private String prefix = "project/organization";

    @Autowired
    private IConstructionOrganizationService constructionOrganizationService;

    @RequiresPermissions("project:organization:view")
    @GetMapping()
    public String organization()
    {
        return prefix + "/organization";
    }

    /**
     * 查询施工单位列表
     */
    @RequiresPermissions("project:organization:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConstructionOrganization constructionOrganization)
    {
        startPage();
        List<ConstructionOrganization> list = constructionOrganizationService.selectConstructionOrganizationList(constructionOrganization);
        return getDataTable(list);
    }

    /**
     * 导出施工单位列表
     */
    @RequiresPermissions("project:organization:export")
    @Log(title = "施工单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ConstructionOrganization constructionOrganization)
    {
        List<ConstructionOrganization> list = constructionOrganizationService.selectConstructionOrganizationList(constructionOrganization);
        ExcelUtil<ConstructionOrganization> util = new ExcelUtil<ConstructionOrganization>(ConstructionOrganization.class);
        return util.exportExcel(list, "施工单位数据");
    }

    /**
     * 下载模板
     *
     * @return
     */
    @RequiresPermissions("project:organization:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ConstructionOrganization> util = new ExcelUtil<>(ConstructionOrganization.class);
        return util.importTemplateExcel("施工单位");
    }

    /**
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */

    @RequiresPermissions("project:organization:import")
    @PostMapping("/importOrganization")
    @ResponseBody
    public AjaxResult importOrganization(MultipartFile file, boolean updateSupport)throws Exception{
        ExcelUtil<ConstructionOrganization> util = new ExcelUtil<>(ConstructionOrganization.class);
        List<ConstructionOrganization> organizationList = util.importExcel(file.getInputStream(), 1);
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = constructionOrganizationService.importOrganization(organizationList, updateSupport, operName);
        return AjaxResult.success(message);

    }


    /**
     * 新增施工单位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存施工单位
     */
    @RequiresPermissions("project:organization:add")
    @Log(title = "施工单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ConstructionOrganization constructionOrganization)
    {
        return toAjax(constructionOrganizationService.insertConstructionOrganization(constructionOrganization));
    }

    /**
     * 修改施工单位
     */
    @RequiresPermissions("project:organization:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ConstructionOrganization constructionOrganization = constructionOrganizationService.selectConstructionOrganizationById(id);
        mmap.put("constructionOrganization", constructionOrganization);
        return prefix + "/edit";
    }

    /**
     * 修改保存施工单位
     */
    @RequiresPermissions("project:organization:edit")
    @Log(title = "施工单位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ConstructionOrganization constructionOrganization)
    {
        return toAjax(constructionOrganizationService.updateConstructionOrganization(constructionOrganization));
    }

    /**
     * 删除施工单位
     */
    @RequiresPermissions("project:organization:remove")
    @Log(title = "施工单位", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(constructionOrganizationService.deleteConstructionOrganizationByIds(ids));
    }

    @GetMapping("/queryOrganizationIdName")
    @ResponseBody
    public AjaxResult queryOrganizationIdName(){
        List<IdName> idNames = constructionOrganizationService.selectOrganizationName();
        return AjaxResult.success(idNames);
    }
}
